﻿using CommonEntity;
using CommonLibary;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace NetCroe6WebAPITestOneBusiness
{
    public class BaseBusiness
    {
        LoggerSingletonHelper _loggerSingletonHelper;

        public BaseBusiness(LoggerSingletonHelper loggerSingletonHelper)
        {
            _loggerSingletonHelper = loggerSingletonHelper;
        }

        internal TResult DoFunc<TResult>(Func<TResult> func) where TResult : new()
        {
            TResult result = new TResult();
            try
            {
                result = func();
                if (result is ViewModelBase)
                {
                    ViewModelBase viewBase = result as ViewModelBase;
                    if (viewBase.code == 0)
                        viewBase.code = HttpStatusCode.OK;
                }
            }
            catch (System.Data.Common.DbException ex)
            {
                if (result is ViewModelBase)
                {
                    ViewModelBase viewBase = result as ViewModelBase;
                    viewBase.code = HttpStatusCode.InternalServerError;
                    viewBase.message = ex.Message;
                }
            }
            catch (Exception ex)
            {
                if (result is ViewModelBase)
                {
                    ViewModelBase viewBase = result as ViewModelBase;
                    viewBase.code = HttpStatusCode.InternalServerError;
                    viewBase.message = ex.Message;
                }
            }

            return result;
        }

        internal async Task<TResult> DoFuncAsync<TResult>(Func<Task<TResult>> func) where TResult : new()
        {
            TResult result = new TResult();
            try
            {
                result = await func();
                if (result is ViewModelBase)
                {
                    ViewModelBase viewBase = result as ViewModelBase;
                    if (viewBase.code == 0)
                        viewBase.code = HttpStatusCode.OK;
                }
            }
            catch (System.Data.Common.DbException ex)
            {
                ViewModelBase viewBase = result as ViewModelBase;
                
                viewBase.code = HttpStatusCode.InternalServerError;
                viewBase.message = ex.Message;

            }
            catch (Exception ex)
            {
                ViewModelBase viewBase = result as ViewModelBase;
                viewBase.code = HttpStatusCode.InternalServerError;
                viewBase.message = ex.Message;
            }
            return result;

        }

        public T toObject<T>(Object obj)
        {
            string objstr = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(objstr);
        }

        public T toObject<T>(String objstr)
        {
            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(objstr);
        }
    }
}
